package com.wyzz.hr.util.excel;

import java.io.FileOutputStream;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public abstract class IExportToExcel {

	public IExportToExcel() throws Exception {
		sheetDefaultName = "sheet";
		statisticSheetDefaultName = "stattistic";
		format = new ExcelFormat();
		append = true;
		setFormat(new ExcelFormat());
	}

	public IExportToExcel(String filePathName) throws Exception {
		sheetDefaultName = "sheet";
		statisticSheetDefaultName = "stattistic";
		format = new ExcelFormat();
		append = true;
		setFilePathName(filePathName);
		setFormat(new ExcelFormat());
	}

	public IExportToExcel(String filePathName, ExcelFormat format)
			throws Exception {
		sheetDefaultName = "sheet";
		statisticSheetDefaultName = "stattistic";
		this.format = new ExcelFormat();
		append = true;
		setFilePathName(filePathName);
		setFormat(format);
	}

	public void exportOnce() throws Exception {
		openFile();
		buildContent();
		closeFile();
	}

	public void openFile() throws Exception {
		fileOutputStream = new FileOutputStream(filePathName);
		writableWorkbook = Workbook.createWorkbook(fileOutputStream);
	}

	public void closeFile() throws Exception {
		writableWorkbook.close();
		writableWorkbook = null;
		fileOutputStream.close();
		fileOutputStream = null;
	}

	protected abstract void buildContent() throws Exception;

	public WritableSheet createSheet(String sheetName, int index,
			WritableWorkbook wb) {
		String sNNew = sheetName.replaceAll("[\\?\\*\\[\\]\\:\\/\\\\]", " ");
		return wb.createSheet(sNNew, index);
	}

	public String getFilePathName() {
		return filePathName;
	}

	public void setFilePathName(String filePathName) {
		this.filePathName = filePathName;
	}

	public ExcelFormat getFormat() {
		return format;
	}

	public void setFormat(ExcelFormat format) throws Exception {
		if (format == null)
			this.format = new ExcelFormat();
		else
			this.format = format;
	}

	public String getSheetDefaultName() {
		return sheetDefaultName;
	}

	public String getStatisticSheetDefaultName() {
		return statisticSheetDefaultName;
	}

	public void setSheetDefaultName(String sheetDefaultName) {
		this.sheetDefaultName = sheetDefaultName;
	}

	public void setStatisticSheetDefaultName(String statisticSheetDefaultName) {
		this.statisticSheetDefaultName = statisticSheetDefaultName;
	}

	protected FileOutputStream fileOutputStream;
	protected WritableWorkbook writableWorkbook;
	protected String filePathName;
	protected String sheetDefaultName;
	protected String statisticSheetDefaultName;
	protected ExcelFormat format;
	protected boolean append;
	public static final String SheetNameNotAllowedCharcater = "?*[]:/\\";
	public static final int rowHeightRate = 20;
}
